package it.fuzzylogics.fuzzywidgetsLibrary.client.FuzzyRoundedPanel;

import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;


public class FRoundPanel extends AbsolutePanel {
    Image leftTopImg;
    Image rightTopImg;
    Image leftBottimImg;
    Image rightBottomImg;

    AbsolutePanel leftPanel;
    AbsolutePanel topPanel;
    AbsolutePanel rightPanel;
    AbsolutePanel bottomPanel;

    AbsolutePanel center;

    Widget content = null;

    int imgWidth;
    int imgHeight;

    int contentX;
    int contentY;

    boolean adjustForIE;

    public FRoundPanel(String str1, String str2, String str3, String str4, int w, int h) {
        this(str1, str2, str3, str4, w, h, false);

    }

    public FRoundPanel(String str1, String str2, String str3, String str4, int w, int h, boolean adjustForIE) {

        this.adjustForIE = adjustForIE;

        imgWidth = w;
        imgHeight = h;
        leftTopImg = new Image(str1);
        rightTopImg = new Image(str2);
        leftBottimImg = new Image(str3);
        rightBottomImg = new Image(str4);

        add(leftTopImg);
        add(rightTopImg);
        add(leftBottimImg);
        add(rightBottomImg);

        leftPanel = new AbsolutePanel();
        topPanel = new AbsolutePanel();
        rightPanel = new AbsolutePanel();
        bottomPanel = new AbsolutePanel();

        add(leftPanel);
        add(topPanel);
        add(rightPanel);
        add(bottomPanel);

        center = new AbsolutePanel();
        add(center);

    }

    public FRoundPanel(Widget content, int contentX, int contentY, String str1, String str2, String str3, String str4, int w, int h, boolean adjustForIE) {

        this.adjustForIE = adjustForIE;
        this.content = content;
        this.contentX = contentX;
        this.contentY = contentY;
        center = new AbsolutePanel();
        //  center.setStyleName("leftPanel");
        add(center);

        add(content);
        contentX = 0;
        contentY = 0;
        imgWidth = w;
        imgHeight = h;
        leftTopImg = new Image(str1);
        leftTopImg.setPixelSize(w, h);
        rightTopImg = new Image(str2);
        rightTopImg.setPixelSize(w, h);
        leftBottimImg = new Image(str3);
        leftBottimImg.setPixelSize(w, h);
        rightBottomImg = new Image(str4);
        rightBottomImg.setPixelSize(w, h);

        add(leftTopImg);
        add(rightTopImg);
        add(leftBottimImg);
        add(rightBottomImg);

        leftPanel = new AbsolutePanel();
        //  leftPanel.setStyleName("leftPanel");
        topPanel = new AbsolutePanel();
        //       topPanel.setStyleName("leftPanel");
        rightPanel = new AbsolutePanel();
        //      rightPanel.setStyleName("leftPanel");
        bottomPanel = new AbsolutePanel();
        //      bottomPanel.setStyleName("bottomPanel");


        add(leftPanel);
        add(topPanel);
        add(rightPanel);
        add(bottomPanel);


    }

    public void insertContent(Widget content, int x, int y) {
        contentX = x;
        contentY = y;
        this.content = content;
        add(content, x, y);

    }

    public void setPixelSize(int w, int h) {
        super.setPixelSize(w, h);
        int offset = 0;
        int size = 0;
        if (adjustForIE && getClientBrowserName().indexOf("Microsoft Internet Explorer") != -1) {
            offset = -3;
            size = 6;
        }

        setWidgetPosition(leftTopImg, 0, 0);
        setWidgetPosition(rightTopImg, w - imgWidth, 0);
        setWidgetPosition(leftBottimImg, 0, h - imgHeight + offset);
        setWidgetPosition(rightBottomImg, w - imgWidth, h - imgHeight + offset);

        setWidgetPosition(leftPanel, 0, imgHeight - offset);
        leftPanel.setPixelSize(imgWidth, h - (2 * imgHeight) - size);

        setWidgetPosition(topPanel, imgWidth, 0);
        topPanel.setPixelSize(w - (2 * imgWidth), imgHeight);

        setWidgetPosition(rightPanel, w - imgWidth, imgHeight - offset);
        rightPanel.setPixelSize(imgWidth, h - (2 * imgHeight) - size);

        setWidgetPosition(bottomPanel, imgWidth, h - imgHeight);
        bottomPanel.setPixelSize(w - (2 * imgWidth), imgHeight);

        setWidgetPosition(center, imgWidth, imgHeight);
        center.setPixelSize(w - (2 * imgWidth), h - (2 * imgHeight));
        if (content != null) {
            setWidgetPosition(content, contentX, contentY);
            content.setPixelSize(w - 2 * contentX, h - 2 * contentY);
        }

    }

    public void setLeftBorderStyle(String style) {
        leftPanel.setStyleName(style);
    }

    public void setTopBorderStyle(String style) {
        topPanel.setStyleName(style);
    }

    public void setRightBorderStyle(String style) {
        rightPanel.setStyleName(style);
    }

    public void setBottomBorderStyle(String style) {
        bottomPanel.setStyleName(style);
    }

    public void setCenterStyle(String style) {
        center.setStyleName(style);
    }


    private native String getClientBrowserName()/*-{
     return navigator.appName;
    }-*/;


}



